<!DOCTYPE html>



  


<html class="theme-next pisces use-motion" lang="">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/blog/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/blog/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/blog/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/blog/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT" />










<meta name="description" content="####1. 开发规范Mapper接口开发方法只需要程序员编写Mapper接口（相当于Dao接口），由Mybatis框架根据接口定义创建接口的动态代理对象，代理对象的方法体同上边Dao接口实现类方法。Mapper接口开发需要遵循以下规范：  Mapper.xml文件中的namespace与mapper接口的类路径相同，即namespace必须是接口的全限定名。 Mapper接口方法名和Mapper">
<meta name="keywords" content="Android、iOS、Web、React Native">
<meta property="og:type" content="article">
<meta property="og:title" content="JavaEE-Mybatis-Mapper使用">
<meta property="og:url" content="http://yoursite.com/blog/2018/08/06/JakartaEE-Framework/JavaEE-Mybatis-Mapper使用/index.html">
<meta property="og:site_name" content="凌浩雨_博客">
<meta property="og:description" content="####1. 开发规范Mapper接口开发方法只需要程序员编写Mapper接口（相当于Dao接口），由Mybatis框架根据接口定义创建接口的动态代理对象，代理对象的方法体同上边Dao接口实现类方法。Mapper接口开发需要遵循以下规范：  Mapper.xml文件中的namespace与mapper接口的类路径相同，即namespace必须是接口的全限定名。 Mapper接口方法名和Mapper">
<meta property="og:locale" content="default">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-ee60f8044b14e3a0.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-d475187ff2575871.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-8c09e5c74b9fcd3b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-6b60375fb8f406a7.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-6904b6711fd5e615.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-207fc73732920f44.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-d87eedb91fbe2edb.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-7accff147c80626c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-51e5eb4413cea246.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-5240d03899d7dced.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-3975357197f9cf2b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-b49278b2186c458d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-dc70a48fc254b867.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-67614c4007493512.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-494f7042869b9ddd.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-806ce82ef31bd441.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-14d28cb3a500d498.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-d25ed0bda09f84ee.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-88db18f044c0c6f6.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-8cfabb3620b5e67e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-2f015dc199eb2b2f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-18a8122184cf91bb.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-5b209fab945d2e5c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:updated_time" content="2018-08-06T10:51:20.411Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="JavaEE-Mybatis-Mapper使用">
<meta name="twitter:description" content="####1. 开发规范Mapper接口开发方法只需要程序员编写Mapper接口（相当于Dao接口），由Mybatis框架根据接口定义创建接口的动态代理对象，代理对象的方法体同上边Dao接口实现类方法。Mapper接口开发需要遵循以下规范：  Mapper.xml文件中的namespace与mapper接口的类路径相同，即namespace必须是接口的全限定名。 Mapper接口方法名和Mapper">
<meta name="twitter:image" content="https://upload-images.jianshu.io/upload_images/3110861-ee60f8044b14e3a0.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/blog/',
    scheme: 'Pisces',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: 'Author'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/blog/2018/08/06/JakartaEE-Framework/JavaEE-Mybatis-Mapper使用/"/>





  <title>JavaEE-Mybatis-Mapper使用 | 凌浩雨_博客</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="default">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/blog/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">凌浩雨_博客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/blog/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            Home
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/blog/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            Archives
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/blog/blog/2018/08/06/JakartaEE-Framework/JavaEE-Mybatis-Mapper使用/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="凌浩雨">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blog/images/avatar.jpeg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="凌浩雨_博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">JavaEE-Mybatis-Mapper使用</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2018-08-06T18:01:38+08:00">
                2018-08-06
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>####1. 开发规范<br>Mapper接口开发方法只需要程序员编写Mapper接口（相当于Dao接口），由Mybatis框架根据接口定义创建接口的动态代理对象，代理对象的方法体同上边Dao接口实现类方法。<br>Mapper接口开发需要遵循以下规范：</p>
<ul>
<li>Mapper.xml文件中的namespace与mapper接口的类路径相同，即namespace必须是接口的全限定名。</li>
<li>Mapper接口方法名和Mapper.xml中定义的每个statement的id相同。</li>
<li>Mapper接口方法的输入参数类型和mapper.xml中定义的每个sql的parameterType的类型相同。</li>
<li>Mapper接口方法的输出参数类型和mapper.xml中定义的每个sql的resultType的类型相同。<a id="more"></a>
</li>
</ul>
<p>####2. 简单使用Mapper<br><strong>本项目数据库基于<a href="https://www.jianshu.com/p/8fc176b2e13b" target="_blank" rel="noopener">JavaEE Mybatis使用</a>文章</strong></p>
<p>######1). 编写Mapper接口<br>在工程的src目录下新建一个com.mazaiting.mapper包，并在该包下创建一个Mapper接口——UserMapper.java<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">public interface UserMapper &#123;</span><br><span class="line">	User getUserById(int id);</span><br><span class="line">	List&lt;User&gt; getUserByName(String username);</span><br><span class="line">	void addUser(User user);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>接口定义有如下特点：</p>
<ul>
<li>mapper接口方法名和mapper.xml中定义的statement的id相同。</li>
<li>mapper接口方法的输入参数类型和mapper.xml中定义的statement的parameterType的类型相同。</li>
<li>mapper接口方法的输出参数类型和mapper.xml中定义的statement的resultType的类型相同。<br>######2). 编写Mapper.xml(映射文件)<br>在config源码目录下新建一个mapper的普通文件夹，该文件夹专门用于存放映射文件。然后在该文件夹下创建一个名为mapper.xml的映射文件<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span><br><span class="line">&lt;!DOCTYPE mapper PUBLIC &quot;-//mybatis.org//DTD Mapper 3.0//EN&quot; &quot;http://mybatis.org/dtd/mybatis-3-mapper.dtd&quot;&gt;</span><br><span class="line">&lt;mapper namespace=&quot;com.mazaiting.mapper.UserMapper&quot;&gt;</span><br><span class="line">	&lt;select id=&quot;getUserById&quot; parameterType=&quot;int&quot; resultType=&quot;com.mazaiting.po.User&quot;&gt;</span><br><span class="line">		select * from user where id = #&#123;id&#125;</span><br><span class="line">	&lt;/select&gt;</span><br><span class="line">	</span><br><span class="line">	&lt;select id=&quot;getUserByName&quot; parameterType=&quot;string&quot; resultType=&quot;com.mazaiting.po.User&quot;&gt;</span><br><span class="line">		select * from user where username like &apos;%$&#123;value&#125;%&apos;</span><br><span class="line">	&lt;/select&gt;</span><br><span class="line">	</span><br><span class="line">	&lt;insert id=&quot;addUser&quot; parameterType=&quot;com.mazaiting.po.User&quot;&gt;</span><br><span class="line">		&lt;selectKey keyProperty=&quot;id&quot; resultType=&quot;int&quot; order=&quot;AFTER&quot;&gt;</span><br><span class="line">			select LAST_INSERT_ID() </span><br><span class="line">		&lt;/selectKey&gt;</span><br><span class="line">		insert into user (username,birthday,sex,address) values(#&#123;username&#125;,#&#123;birthday&#125;,#&#123;sex&#125;,#&#123;address&#125;)</span><br><span class="line">	&lt;/insert&gt;</span><br><span class="line">&lt;/mapper&gt;</span><br></pre></td></tr></table></figure>
</li>
</ul>
<p>######3). 加载mapper.xml映射文件<br>在SqlMapConfig.xml文件添加如下配置：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&lt;mapper resource=&quot;mapper/mapper.xml&quot;/&gt;</span><br></pre></td></tr></table></figure></p>
<p>SqlMapConfig.xml内容如下：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span><br><span class="line">&lt;!DOCTYPE configuration PUBLIC &quot;-//mybatis.org//DTD Config 3.0//EN&quot; &quot;http://mybatis.org/dtd/mybatis-3-config.dtd&quot;&gt;</span><br><span class="line">&lt;configuration&gt;</span><br><span class="line">    &lt;!-- 和spring整合后environments配置将废除 --&gt;</span><br><span class="line">    &lt;environments default=&quot;development&quot;&gt;</span><br><span class="line">        &lt;environment id=&quot;development&quot;&gt;</span><br><span class="line">            &lt;!-- 使用jdbc事务管理 --&gt;</span><br><span class="line">            &lt;transactionManager type=&quot;JDBC&quot; /&gt;</span><br><span class="line">            &lt;!-- 数据库连接池 --&gt;</span><br><span class="line">            &lt;dataSource type=&quot;POOLED&quot;&gt;</span><br><span class="line">                &lt;property name=&quot;driver&quot; value=&quot;com.mysql.jdbc.Driver&quot; /&gt;</span><br><span class="line">                &lt;property name=&quot;url&quot; value=&quot;jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8&quot; /&gt;</span><br><span class="line">                &lt;property name=&quot;username&quot; value=&quot;root&quot; /&gt;</span><br><span class="line">                &lt;property name=&quot;password&quot; value=&quot;root&quot; /&gt;</span><br><span class="line">            &lt;/dataSource&gt;</span><br><span class="line">        &lt;/environment&gt;</span><br><span class="line">    &lt;/environments&gt;</span><br><span class="line">    </span><br><span class="line">    &lt;!-- 添加的内容 --&gt;</span><br><span class="line">    &lt;mappers&gt;</span><br><span class="line">    	&lt;!-- resource是基于classpath来查找的 --&gt;</span><br><span class="line">    	&lt;mapper resource=&quot;sqlmap/user.xml&quot;/&gt;</span><br><span class="line">    	&lt;mapper resource=&quot;mapper/mapper.xml&quot;/&gt;</span><br><span class="line">    &lt;/mappers&gt;</span><br><span class="line">&lt;/configuration&gt;</span><br></pre></td></tr></table></figure></p>
<p>######4). 编写测试程序<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br></pre></td><td class="code"><pre><span class="line">public class UserMapperTest &#123;</span><br><span class="line">	// 单例工厂</span><br><span class="line">	private SqlSessionFactory factory;</span><br><span class="line">	@Before</span><br><span class="line">	public void init() throws IOException &#123;</span><br><span class="line">		// 1. 创建SqlSessionFactoryBuilder对象</span><br><span class="line">		SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();</span><br><span class="line">		// 2. 加载配置文件</span><br><span class="line">		InputStream inputStream = Resources.getResourceAsStream(&quot;SqlMapConfig.xml&quot;);</span><br><span class="line">		// 3. 创建SqlSessionFactory对象</span><br><span class="line">		factory = builder.build(inputStream);</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	@Test</span><br><span class="line">	public void testGetUserById() &#123;</span><br><span class="line">		// 和Spring整合后就省略了</span><br><span class="line">		SqlSession session = factory.openSession();</span><br><span class="line">		// 获得代理对象(和Spring整合后只需要通过Spring容器拿到Usermapper接口的搭理对象就可以了)</span><br><span class="line">		UserMapper userMapper = session.getMapper(UserMapper.class);</span><br><span class="line">		User user = userMapper.getUserById(10);</span><br><span class="line">		System.out.println(user);</span><br><span class="line">		// 和Spring整合后就省略了</span><br><span class="line">		session.close();</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	@Test</span><br><span class="line">	public void testGetUserByName() &#123;</span><br><span class="line">		SqlSession session = factory.openSession();</span><br><span class="line">		UserMapper mapper = session.getMapper(UserMapper.class);</span><br><span class="line">		List&lt;User&gt; list = mapper.getUserByName(&quot;张&quot;);</span><br><span class="line">		for(User user : list) &#123;</span><br><span class="line">			System.out.println(user);</span><br><span class="line">		&#125;</span><br><span class="line">		session.close();</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	@Test</span><br><span class="line">	public void testAddUser()&#123;</span><br><span class="line">		SqlSession session = factory.openSession();</span><br><span class="line">		UserMapper mapper = session.getMapper(UserMapper.class);</span><br><span class="line">		User user = new User();</span><br><span class="line">		user.setUsername(&quot;凌浩雨&quot;);</span><br><span class="line">		user.setSex(&quot;男&quot;);</span><br><span class="line">		user.setBirthday(new Date());</span><br><span class="line">		user.setAddress(&quot;理化所&quot;);</span><br><span class="line">		mapper.addUser(user);</span><br><span class="line">		System.out.println(user.getId());</span><br><span class="line">		session.commit();</span><br><span class="line">		session.close();</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>######5). 打印结果：<br><strong>testGetUserById</strong><br><img src="https://upload-images.jianshu.io/upload_images/3110861-ee60f8044b14e3a0.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图1.png"></p>
<p><strong>testGetUserByName</strong><br><img src="https://upload-images.jianshu.io/upload_images/3110861-d475187ff2575871.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图2.png"></p>
<p><strong>testAddUser</strong><br><img src="https://upload-images.jianshu.io/upload_images/3110861-8c09e5c74b9fcd3b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图3.png"></p>
<p>######6). 总结</p>
<ul>
<li>selectOne和selectList<br>动态代理对象调用sqlSession.selectOne()和sqlSession.selectList()是根据mapper接口方法的返回值决定，如果返回list则调用selectList方法，如果返回单个对象则调用selectOne方法。</li>
<li>namespace<br>mybatis官方推荐使用mapper代理方法开发mapper接口，程序员不用编写mapper接口实现类，使用mapper代理方法时，输入参数可以使用pojo包装对象或map对象，保证dao的通用性。</li>
</ul>
<p>####3. SqlMapConfig.xml配置文件</p>
<p>######1). 配置内容<br>SqlMapConfig.xml文件中配置的内容和顺序如下：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">1&gt;. properties（属性）</span><br><span class="line">2&gt;. settings（全局配置参数）</span><br><span class="line">3&gt;. typeAliases（类型别名）</span><br><span class="line">4&gt;. typeHandlers（类型处理器）</span><br><span class="line">5&gt;. objectFactory（对象工厂）</span><br><span class="line">6&gt;. plugins（插件）</span><br><span class="line">7&gt;. environments（环境集合属性对象） </span><br><span class="line">	I.environment（环境子属性对象） </span><br><span class="line">		1&gt;&gt;.transactionManager（事务管理）</span><br><span class="line">		2&gt;&gt;.dataSource（数据源）</span><br><span class="line">8&gt;. mappers（映射器）</span><br></pre></td></tr></table></figure></p>
<p>######2). properties(属性)<br>在SqlMapConfig.xml配置文件中，我们可把数据库连接信息配置到properties标签当中<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">&lt;!-- 配置属性 --&gt;</span><br><span class="line">&lt;properties&gt;</span><br><span class="line">	&lt;property name=&quot;jdbc.driver&quot; value=&quot;com.mysql.jdbc.Driver&quot;/&gt;</span><br><span class="line">	&lt;property name=&quot;jdbc.url&quot; value=&quot;jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8&quot;/&gt;</span><br><span class="line">&lt;/properties&gt;</span><br></pre></td></tr></table></figure></p>
<p>SqlMapConfig.xml文件内容:<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span><br><span class="line">&lt;!DOCTYPE configuration PUBLIC &quot;-//mybatis.org//DTD Config 3.0//EN&quot; &quot;http://mybatis.org/dtd/mybatis-3-config.dtd&quot;&gt;</span><br><span class="line">&lt;configuration&gt;</span><br><span class="line">	&lt;!-- 配置属性 --&gt;</span><br><span class="line">	&lt;properties&gt;</span><br><span class="line">		&lt;property name=&quot;jdbc.driver&quot; value=&quot;com.mysql.jdbc.Driver&quot;/&gt;</span><br><span class="line">		&lt;property name=&quot;jdbc.url&quot; value=&quot;jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8&quot;/&gt;</span><br><span class="line">	&lt;/properties&gt;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    &lt;!-- 和spring整合后environments配置将废除 --&gt;</span><br><span class="line">    &lt;environments default=&quot;development&quot;&gt;</span><br><span class="line">        &lt;environment id=&quot;development&quot;&gt;</span><br><span class="line">            &lt;!-- 使用jdbc事务管理 --&gt;</span><br><span class="line">            &lt;transactionManager type=&quot;JDBC&quot; /&gt;</span><br><span class="line">            &lt;!-- 数据库连接池 --&gt;</span><br><span class="line">            &lt;dataSource type=&quot;POOLED&quot;&gt;</span><br><span class="line">                &lt;property name=&quot;driver&quot; value=&quot;$&#123;jdbc.driver&#125;&quot; /&gt;</span><br><span class="line">                &lt;property name=&quot;url&quot; value=&quot;$&#123;jdbc.url&#125;&quot; /&gt;</span><br><span class="line">                &lt;property name=&quot;username&quot; value=&quot;root&quot; /&gt;</span><br><span class="line">                &lt;property name=&quot;password&quot; value=&quot;root&quot; /&gt;</span><br><span class="line">            &lt;/dataSource&gt;</span><br><span class="line">        &lt;/environment&gt;</span><br><span class="line">    &lt;/environments&gt;</span><br><span class="line">    </span><br><span class="line">    &lt;!-- 添加的内容 --&gt;</span><br><span class="line">    &lt;mappers&gt;</span><br><span class="line">    	&lt;!-- resource是基于classpath来查找的 --&gt;</span><br><span class="line">    	&lt;mapper resource=&quot;sqlmap/user.xml&quot;/&gt;</span><br><span class="line">    	&lt;mapper resource=&quot;mapper/mapper.xml&quot;/&gt;</span><br><span class="line">    &lt;/mappers&gt;</span><br><span class="line">&lt;/configuration&gt;</span><br></pre></td></tr></table></figure></p>
<p>将数据库连接信息配置到一个java属性文件中，然后再来引用其中的配置信息。我按照这种指导思想在classpath下定义一个db.properties文件.<br><strong>db.properties</strong><br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">jdbc.driver=com.mysql.jdbc.Driver</span><br><span class="line">jdbc.url=jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8</span><br><span class="line">jdbc.username=root</span><br><span class="line">jdbc.password=root</span><br></pre></td></tr></table></figure></p>
<p>SqlMapConfig.xml文件内容:<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span><br><span class="line">&lt;!DOCTYPE configuration PUBLIC &quot;-//mybatis.org//DTD Config 3.0//EN&quot; &quot;http://mybatis.org/dtd/mybatis-3-config.dtd&quot;&gt;</span><br><span class="line">&lt;configuration&gt;</span><br><span class="line">	&lt;!-- 配置属性 --&gt;</span><br><span class="line">	&lt;properties resource=&quot;db.properties&quot;&gt;</span><br><span class="line">		&lt;!-- &lt;property name=&quot;jdbc.driver&quot; value=&quot;com.mysql.jdbc.Driver&quot;/&gt;</span><br><span class="line">		&lt;property name=&quot;jdbc.url&quot; value=&quot;jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8&quot;/&gt; --&gt;</span><br><span class="line">		&lt;property name=&quot;jdbc.driver&quot; value=&quot;$&#123;jdbc.driver&#125;&quot;/&gt;</span><br><span class="line">		&lt;property name=&quot;jdbc.url&quot; value=&quot;$&#123;jdbc.url&#125;&quot;/&gt;</span><br><span class="line">	&lt;/properties&gt;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    &lt;!-- 和spring整合后environments配置将废除 --&gt;</span><br><span class="line">    &lt;environments default=&quot;development&quot;&gt;</span><br><span class="line">        &lt;environment id=&quot;development&quot;&gt;</span><br><span class="line">            &lt;!-- 使用jdbc事务管理 --&gt;</span><br><span class="line">            &lt;transactionManager type=&quot;JDBC&quot; /&gt;</span><br><span class="line">            &lt;!-- 数据库连接池 --&gt;</span><br><span class="line">            &lt;dataSource type=&quot;POOLED&quot;&gt;</span><br><span class="line">                &lt;property name=&quot;driver&quot; value=&quot;$&#123;jdbc.driver&#125;&quot; /&gt;</span><br><span class="line">                &lt;property name=&quot;url&quot; value=&quot;$&#123;jdbc.url&#125;&quot; /&gt;</span><br><span class="line">                &lt;property name=&quot;username&quot; value=&quot;$&#123;jdbc.username&#125;&quot; /&gt;</span><br><span class="line">                &lt;property name=&quot;password&quot; value=&quot;$&#123;jdbc.password&#125;&quot; /&gt;</span><br><span class="line">            &lt;/dataSource&gt;</span><br><span class="line">        &lt;/environment&gt;</span><br><span class="line">    &lt;/environments&gt;</span><br><span class="line">    </span><br><span class="line">    &lt;!-- 添加的内容 --&gt;</span><br><span class="line">    &lt;mappers&gt;</span><br><span class="line">    	&lt;!-- resource是基于classpath来查找的 --&gt;</span><br><span class="line">    	&lt;mapper resource=&quot;sqlmap/user.xml&quot;/&gt;</span><br><span class="line">    	&lt;mapper resource=&quot;mapper/mapper.xml&quot;/&gt;</span><br><span class="line">    &lt;/mappers&gt;</span><br><span class="line">&lt;/configuration&gt;</span><br></pre></td></tr></table></figure></p>
<p>######3). typeAliases（类型别名）</p>
<ul>
<li>mybatis支持别名</li>
</ul>
<table>
<thead>
<tr>
<th>别名</th>
<th>映射的类型</th>
</tr>
</thead>
<tbody>
<tr>
<td>_byte</td>
<td>byte</td>
</tr>
<tr>
<td>_long</td>
<td>long</td>
</tr>
<tr>
<td>_short</td>
<td>short</td>
</tr>
<tr>
<td>_int</td>
<td>int</td>
</tr>
<tr>
<td>_integer</td>
<td>int</td>
</tr>
<tr>
<td>_double</td>
<td>double</td>
</tr>
<tr>
<td>_float</td>
<td>float</td>
</tr>
<tr>
<td>_boolean</td>
<td>boolean</td>
</tr>
<tr>
<td>string</td>
<td>String</td>
</tr>
<tr>
<td>byte</td>
<td>Byte</td>
</tr>
<tr>
<td>long</td>
<td>Long</td>
</tr>
<tr>
<td>short</td>
<td>Short</td>
</tr>
<tr>
<td>int</td>
<td>Integer</td>
</tr>
<tr>
<td>integer</td>
<td>Integer</td>
</tr>
<tr>
<td>double</td>
<td>Double</td>
</tr>
<tr>
<td>float</td>
<td>Float</td>
</tr>
<tr>
<td>boolean</td>
<td>Boolean</td>
</tr>
<tr>
<td>date</td>
<td>Date</td>
</tr>
<tr>
<td>decimal</td>
<td>BigDecimal</td>
</tr>
<tr>
<td>bigdecimal</td>
<td>BigDecimal</td>
</tr>
<tr>
<td>map</td>
<td>Map</td>
</tr>
</tbody>
</table>
<ul>
<li>自定义别名<br>在SqlMapConfig.xml文件中添加：<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">&lt;!-- 配置pojo的别名 --&gt;</span><br><span class="line">&lt;typeAliases&gt;</span><br><span class="line">	&lt;!-- 单个定义别名，别名不区分大小写 --&gt;</span><br><span class="line">	&lt;typeAlias type=&quot;com.mazaiting.po.User&quot; alias=&quot;User&quot;/&gt;</span><br><span class="line">&lt;/typeAliases&gt;</span><br></pre></td></tr></table></figure>
</li>
</ul>
<p>则mapper.xml文件可以修改为：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span><br><span class="line">&lt;!DOCTYPE mapper PUBLIC &quot;-//mybatis.org//DTD Mapper 3.0//EN&quot; &quot;http://mybatis.org/dtd/mybatis-3-mapper.dtd&quot;&gt;</span><br><span class="line">&lt;mapper namespace=&quot;com.mazaiting.mapper.UserMapper&quot;&gt;</span><br><span class="line">	&lt;!-- 应用别名 --&gt;</span><br><span class="line">	&lt;select id=&quot;getUserById&quot; parameterType=&quot;int&quot; resultType=&quot;User&quot;&gt;</span><br><span class="line">		select * from user where id = #&#123;id&#125;</span><br><span class="line">	&lt;/select&gt;</span><br><span class="line">	</span><br><span class="line">	&lt;select id=&quot;getUserByName&quot; parameterType=&quot;string&quot; resultType=&quot;com.mazaiting.po.User&quot;&gt;</span><br><span class="line">		select * from user where username like &apos;%$&#123;value&#125;%&apos;</span><br><span class="line">	&lt;/select&gt;</span><br><span class="line">	</span><br><span class="line">	&lt;insert id=&quot;addUser&quot; parameterType=&quot;com.mazaiting.po.User&quot;&gt;</span><br><span class="line">		&lt;selectKey keyProperty=&quot;id&quot; resultType=&quot;int&quot; order=&quot;AFTER&quot;&gt;</span><br><span class="line">			select LAST_INSERT_ID() </span><br><span class="line">		&lt;/selectKey&gt;</span><br><span class="line">		insert into user (username,birthday,sex,address) values(#&#123;username&#125;,#&#123;birthday&#125;,#&#123;sex&#125;,#&#123;address&#125;)</span><br><span class="line">	&lt;/insert&gt;</span><br><span class="line">&lt;/mapper&gt;</span><br></pre></td></tr></table></figure></p>
<p><strong>注意：resultType属性的值就是User类的别名，且别名是不区分大小写的.</strong></p>
<ul>
<li>批量定义别名<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">&lt;!-- 配置pojo的别名 --&gt;</span><br><span class="line">&lt;typeAliases&gt;</span><br><span class="line">    &lt;!-- 批量别名定义，扫描包的形式创建别名，别名就是类名，且不区分大小写 --&gt;</span><br><span class="line">    &lt;package name=&quot;com.mazaiting.po&quot;/&gt;</span><br><span class="line">&lt;/typeAliases&gt;</span><br></pre></td></tr></table></figure>
</li>
</ul>
<p>######4). SqlMapConfig.xml文件加载mapper.xml文件</p>
<ul>
<li><mapper resource=" "><br>使用相对于类路径的资源，如<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&lt;mapper resource=&quot;sqlmap/user.xml&quot;/&gt;</span><br></pre></td></tr></table></figure>
</mapper></li>
</ul>
<p><img src="https://upload-images.jianshu.io/upload_images/3110861-6b60375fb8f406a7.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图4.png"></p>
<ul>
<li><mapper class=" "><br>使用mapper接口类路径，如：<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&lt;mapper class=&quot;com.mazaiting.mapper.UserMapper&quot;/&gt;</span><br></pre></td></tr></table></figure>
</mapper></li>
</ul>
<p><img src="https://upload-images.jianshu.io/upload_images/3110861-6904b6711fd5e615.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图5.png"><br><strong>注意：此种方法要求mapper接口名称和mapper映射文件名称相同，且放在同一个目录中。</strong></p>
<ul>
<li><package name=""><br>注册指定包下的所有mapper接口，如：<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&lt;package name=&quot;com.mazaiting.mapper&quot;/&gt;</span><br></pre></td></tr></table></figure>
</package></li>
</ul>
<p><img src="https://upload-images.jianshu.io/upload_images/3110861-207fc73732920f44.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图6.png"><br><strong>注意：此种方法要求mapper接口名称和mapper映射文件名称相同，且放在同一个目录中。</strong></p>
<p>####4. 输入映射–parameterType(输入类型)</p>
<p>######1). 传递简单类型<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">&lt;select id=&quot;getUserById&quot; parameterType=&quot;int&quot; resultType=&quot;com.mazaiting.po.User&quot;&gt;</span><br><span class="line">	select * from user where id = #&#123;id&#125;;</span><br><span class="line">&lt;/select&gt;</span><br></pre></td></tr></table></figure></p>
<p><img src="https://upload-images.jianshu.io/upload_images/3110861-d87eedb91fbe2edb.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图7.png"></p>
<p>######2). 传递pojo对象<br>MyBatis使用ognl表达式解析对象字段的值，#{}或者${}括号中的值为pojo属性名称。<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">&lt;insert id=&quot;addUser&quot; parameterType=&quot;com.mazaiting.po.User&quot;&gt;</span><br><span class="line">	insert into user(username,birthday,sex,address) values(#&#123;username&#125;,#&#123;birthday&#125;,#&#123;sex&#125;,#&#123;address&#125;)</span><br><span class="line">&lt;/insert&gt;</span><br></pre></td></tr></table></figure></p>
<p><img src="https://upload-images.jianshu.io/upload_images/3110861-7accff147c80626c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图8.png"></p>
<p>######3). 传递pojo包装对象<br>I. 在com.mazaiting.po包下新建QueryVo类<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">public class QueryVo &#123;</span><br><span class="line">	private User user;</span><br><span class="line">	public void setUser(User user) &#123;</span><br><span class="line">		this.user = user;</span><br><span class="line">	&#125;</span><br><span class="line">	public User getUser() &#123;</span><br><span class="line">		return user;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>II. 在UserMapper.xml文件中添加查询语句<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span><br><span class="line">&lt;!DOCTYPE mapper PUBLIC &quot;-//mybatis.org//DTD Mapper 3.0//EN&quot; &quot;http://mybatis.org/dtd/mybatis-3-mapper.dtd&quot;&gt;</span><br><span class="line">&lt;mapper namespace=&quot;com.mazaiting.mapper.UserMapper&quot;&gt;</span><br><span class="line">	</span><br><span class="line">	&lt;select id=&quot;getUserByQueryVo&quot; parameterType=&quot;queryvo&quot; resultType=&quot;user&quot;&gt;</span><br><span class="line">		select * from user where id = #&#123;user.id&#125;	</span><br><span class="line">	&lt;/select&gt;</span><br><span class="line">	</span><br><span class="line">&lt;/mapper&gt;</span><br></pre></td></tr></table></figure></p>
<p>III. 在UserMapper接口中添加方法<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">public interface UserMapper &#123;</span><br><span class="line">	User getUserByQueryVo(QueryVo queryVo);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>IV. 编写测试代码<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line">public class UserMapperTest &#123;</span><br><span class="line">	// 单例工厂</span><br><span class="line">	private SqlSessionFactory factory;</span><br><span class="line">	@Before</span><br><span class="line">	public void init() throws IOException &#123;</span><br><span class="line">		// 1. 创建SqlSessionFactoryBuilder对象</span><br><span class="line">		SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();</span><br><span class="line">		// 2. 加载配置文件</span><br><span class="line">		InputStream inputStream = Resources.getResourceAsStream(&quot;SqlMapConfig.xml&quot;);</span><br><span class="line">		// 3. 创建SqlSessionFactory对象</span><br><span class="line">		factory = builder.build(inputStream);</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	@Test </span><br><span class="line">	public void testGetUserByQueryVo()&#123;</span><br><span class="line">		SqlSession session = factory.openSession();</span><br><span class="line">		UserMapper mapper = session.getMapper(UserMapper.class);</span><br><span class="line">		QueryVo queryVo = new QueryVo();</span><br><span class="line">		User user = new User();</span><br><span class="line">		user.setId(10);</span><br><span class="line">		queryVo.setUser(user);</span><br><span class="line">		User queryUser = mapper.getUserByQueryVo(queryVo);</span><br><span class="line">		System.out.println(queryUser);</span><br><span class="line">		session.close();</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>V. 打印结果：<br><img src="https://upload-images.jianshu.io/upload_images/3110861-51e5eb4413cea246.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图9.png"></p>
<p>######4). 传递HashMap(传递HashMap在实际开发中用的很少)<br>I. 在UserMapper中添加查询方法<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span><br><span class="line">&lt;!DOCTYPE mapper PUBLIC &quot;-//mybatis.org//DTD Mapper 3.0//EN&quot; &quot;http://mybatis.org/dtd/mybatis-3-mapper.dtd&quot;&gt;</span><br><span class="line">&lt;mapper namespace=&quot;com.mazaiting.mapper.UserMapper&quot;&gt;</span><br><span class="line">   	&lt;!-- 传递HashMap综合查询用户信息 --&gt;</span><br><span class="line">	&lt;select id=&quot;findUserByHashMap&quot; parameterType=&quot;hashmap&quot; resultType=&quot;user&quot;&gt;</span><br><span class="line">		select * from user where id = #&#123;id&#125; and username like &apos;%$&#123;username&#125;%&apos;</span><br><span class="line">	&lt;/select&gt;</span><br><span class="line">&lt;/mapper&gt;</span><br></pre></td></tr></table></figure></p>
<p><strong>注：id和username是HashMap的key。</strong><br>II. 在UserMapper中添加接口方法<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">public interface UserMapper &#123;</span><br><span class="line">	User findUserByHashMap(HashMap&lt;String, Object&gt; map);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>III. 编写测试方法<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">public class UserMapperTest &#123;</span><br><span class="line">	// 单例工厂</span><br><span class="line">	private SqlSessionFactory factory;</span><br><span class="line">	@Before</span><br><span class="line">	public void init() throws IOException &#123;</span><br><span class="line">		// 1. 创建SqlSessionFactoryBuilder对象</span><br><span class="line">		SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();</span><br><span class="line">		// 2. 加载配置文件</span><br><span class="line">		InputStream inputStream = Resources.getResourceAsStream(&quot;SqlMapConfig.xml&quot;);</span><br><span class="line">		// 3. 创建SqlSessionFactory对象</span><br><span class="line">		factory = builder.build(inputStream);</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	@Test</span><br><span class="line">	public void testFindUserByHashMap()&#123;</span><br><span class="line">		SqlSession session = factory.openSession();</span><br><span class="line">		UserMapper mapper = session.getMapper(UserMapper.class);</span><br><span class="line">		HashMap&lt;String, Object&gt; map = new HashMap&lt;&gt;();</span><br><span class="line">		map.put(&quot;id&quot;, 10);</span><br><span class="line">		map.put(&quot;username&quot;, &quot;张&quot;);</span><br><span class="line">		User user = mapper.findUserByHashMap(map);</span><br><span class="line">		System.out.println(user);</span><br><span class="line">		session.close();</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>IV. 打印结果<br><img src="https://upload-images.jianshu.io/upload_images/3110861-5240d03899d7dced.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图10.png"></p>
<p>####5. 输出映射–resultType(输出类型)</p>
<p>######1). 输出简单类型<br>I. 在UserMapper.xml文件中添加配置<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span><br><span class="line">&lt;!DOCTYPE mapper PUBLIC &quot;-//mybatis.org//DTD Mapper 3.0//EN&quot; &quot;http://mybatis.org/dtd/mybatis-3-mapper.dtd&quot;&gt;</span><br><span class="line">&lt;mapper namespace=&quot;com.mazaiting.mapper.UserMapper&quot;&gt;</span><br><span class="line">	&lt;!-- 查询表中的记录数 --&gt;</span><br><span class="line">	&lt;select id=&quot;getUserCount&quot; resultType=&quot;int&quot;&gt;</span><br><span class="line">		select COUNT(*) from user</span><br><span class="line">	&lt;/select&gt;</span><br><span class="line">&lt;/mapper&gt;</span><br></pre></td></tr></table></figure></p>
<p>II. 在UserMapper接口中添加方法<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">public interface UserMapper &#123;</span><br><span class="line">	Integer getUserCount();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>III. 编写测试方法<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">public class UserMapperTest &#123;</span><br><span class="line">	// 单例工厂</span><br><span class="line">	private SqlSessionFactory factory;</span><br><span class="line">	@Before</span><br><span class="line">	public void init() throws IOException &#123;</span><br><span class="line">		// 1. 创建SqlSessionFactoryBuilder对象</span><br><span class="line">		SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();</span><br><span class="line">		// 2. 加载配置文件</span><br><span class="line">		InputStream inputStream = Resources.getResourceAsStream(&quot;SqlMapConfig.xml&quot;);</span><br><span class="line">		// 3. 创建SqlSessionFactory对象</span><br><span class="line">		factory = builder.build(inputStream);</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	@Test</span><br><span class="line">	public void testGetUserCount()&#123;</span><br><span class="line">		SqlSession session = factory.openSession();</span><br><span class="line">		UserMapper mapper = session.getMapper(UserMapper.class);</span><br><span class="line">		int count = mapper.getUserCount();</span><br><span class="line">		System.out.println(&quot;共有 &quot; + count + &quot;条记录&quot;);</span><br><span class="line">		session.close();</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>IV. 打印结果：<br><img src="https://upload-images.jianshu.io/upload_images/3110861-3975357197f9cf2b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图11.png"></p>
<p>######2). 输出pojo对象<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">&lt;insert id=&quot;addUser&quot; parameterType=&quot;com.mazaiting.po.User&quot;&gt;</span><br><span class="line">	&lt;selectKey keyProperty=&quot;id&quot; resultType=&quot;int&quot; order=&quot;AFTER&quot;&gt;</span><br><span class="line">		select LAST_INSERT_ID() </span><br><span class="line">	&lt;/selectKey&gt;</span><br><span class="line">	insert into user (username,birthday,sex,address) values(#&#123;username&#125;,#&#123;birthday&#125;,#&#123;sex&#125;,#&#123;address&#125;)</span><br><span class="line">&lt;/insert&gt;</span><br></pre></td></tr></table></figure></p>
<p><img src="https://upload-images.jianshu.io/upload_images/3110861-b49278b2186c458d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图12.png"></p>
<p>######3). 输出pojo列表<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">&lt;select id=&quot;getUserByName&quot; parameterType=&quot;string&quot; resultType=&quot;com.mazaiting.po.User&quot;&gt;</span><br><span class="line">	select * from user where username like &apos;%$&#123;value&#125;%&apos;</span><br><span class="line">&lt;/select&gt;</span><br></pre></td></tr></table></figure></p>
<p><img src="https://upload-images.jianshu.io/upload_images/3110861-dc70a48fc254b867.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图13.png"></p>
<p>######4). 当pojo中属性名和数据库中字段名不相同时<br><img src="https://upload-images.jianshu.io/upload_images/3110861-67614c4007493512.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图14.png"><br>I. 在com.mazaiting.po包下创建Order.java<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line">public class Order &#123;</span><br><span class="line">	private Integer id;</span><br><span class="line">	private Integer userId;</span><br><span class="line">	private String number;</span><br><span class="line">	private Date createtime;</span><br><span class="line">	private String note;</span><br><span class="line">	public Integer getId() &#123;</span><br><span class="line">		return id;</span><br><span class="line">	&#125;</span><br><span class="line">	public void setId(Integer id) &#123;</span><br><span class="line">		this.id = id;</span><br><span class="line">	&#125;</span><br><span class="line">	public Integer getUserId() &#123;</span><br><span class="line">		return userId;</span><br><span class="line">	&#125;</span><br><span class="line">	public void setUserId(Integer userId) &#123;</span><br><span class="line">		this.userId = userId;</span><br><span class="line">	&#125;</span><br><span class="line">	public String getNumber() &#123;</span><br><span class="line">		return number;</span><br><span class="line">	&#125;</span><br><span class="line">	public void setNumber(String number) &#123;</span><br><span class="line">		this.number = number;</span><br><span class="line">	&#125;</span><br><span class="line">	public Date getCreatetime() &#123;</span><br><span class="line">		return createtime;</span><br><span class="line">	&#125;</span><br><span class="line">	public void setCreatetime(Date createtime) &#123;</span><br><span class="line">		this.createtime = createtime;</span><br><span class="line">	&#125;</span><br><span class="line">	public String getNote() &#123;</span><br><span class="line">		return note;</span><br><span class="line">	&#125;</span><br><span class="line">	public void setNote(String note) &#123;</span><br><span class="line">		this.note = note;</span><br><span class="line">	&#125;</span><br><span class="line">	@Override</span><br><span class="line">	public String toString() &#123;</span><br><span class="line">		return &quot;Order [id=&quot; + id + &quot;, userId=&quot; + userId + &quot;, number=&quot; + number + &quot;, createtime=&quot; + createtime</span><br><span class="line">				+ &quot;, note=&quot; + note + &quot;]&quot;;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>II. 在com.mazaiting.mapper包下创建接口OrderMapper<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">public interface OrderMapper &#123;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>III. 在com.mazaiting.mapper包下创建OrderMapper.xml文件<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span><br><span class="line">&lt;!DOCTYPE mapper PUBLIC &quot;-//mybatis.org//DTD Mapper 3.0//EN&quot; &quot;http://mybatis.org/dtd/mybatis-3-mapper.dtd&quot;&gt;</span><br><span class="line">&lt;mapper namespace=&quot;com.mazaiting.mapper.OrderMapper&quot;&gt;</span><br><span class="line"></span><br><span class="line">	&lt;select id=&quot;getOrderList&quot; resultType=&quot;order&quot;&gt;</span><br><span class="line">		select * from order	</span><br><span class="line">	&lt;/select&gt;	</span><br><span class="line">	</span><br><span class="line">&lt;/mapper&gt;</span><br></pre></td></tr></table></figure></p>
<p>IV. 在OrderMapper接口中添加方法<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">public interface OrderMapper &#123;</span><br><span class="line">	List&lt;Order&gt; getOrderList();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>V. 编写测试方法<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">public class OrderMapperTest &#123;</span><br><span class="line">	SqlSessionFactory factory;</span><br><span class="line">	@Before</span><br><span class="line">	public void init() throws IOException &#123;</span><br><span class="line">		SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();</span><br><span class="line">		InputStream inputStream = Resources.getResourceAsStream(&quot;SqlMapConfig.xml&quot;);</span><br><span class="line">		factory = builder.build(inputStream);</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	@Test</span><br><span class="line">	public void testGetOrderList() &#123;</span><br><span class="line">		SqlSession session = factory.openSession();</span><br><span class="line">		OrderMapper mapper = session.getMapper(OrderMapper.class);</span><br><span class="line">		List&lt;Order&gt; list = mapper.getOrderList();</span><br><span class="line">		for (Order order : list) &#123;</span><br><span class="line">			System.out.println(order);</span><br><span class="line">		&#125;</span><br><span class="line">		session.close();</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>VI. 打印结果<br><img src="https://upload-images.jianshu.io/upload_images/3110861-494f7042869b9ddd.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图15.png"><br>可以看到userId读取出来为null,原因 数据库表中的字段名为user_id.</p>
<p>######5). 获取userId解决办法1<br>将OrderMapper.xml文件中的<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">&lt;select id=&quot;getOrderList&quot; resultType=&quot;order&quot;&gt;</span><br><span class="line">    select * from order </span><br><span class="line">&lt;/select&gt;</span><br></pre></td></tr></table></figure></p>
<p>修改为<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">	&lt;select id=&quot;getOrderList&quot; resultType=&quot;order&quot;&gt;</span><br><span class="line">		select id,user_id userId,number,createtime,note from orders</span><br><span class="line">	&lt;/select&gt;</span><br><span class="line">`</span><br></pre></td></tr></table></figure></p>
<p>执行测试方法，打印结果：<br><img src="https://upload-images.jianshu.io/upload_images/3110861-806ce82ef31bd441.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图16.png"></p>
<p>######6). 获取userId解决办法2– resultMap<br>resultMap可以指定pojo将查询结果映射为pojo，但需要pojo的属性名和sql查询的列名一致方可映射成功。如果sql查询字段名和pojo的属性名不一致，可以通过resultMap将字段名和属性名作一个对应关系 ，resultMap实质上还需要将查询结果映射到pojo对象中。<br>resultMap可以实现将查询结果映射为复杂类型的pojo，比如在查询结果映射对象中包括pojo和list实现一对一查询和一对多查询。 </p>
<ul>
<li>type：指resultMap要映射成的数据类型(返回结果映射的pojo，可以使用别名)。</li>
<li><id>：此属性表示查询结果集的唯一标识，非常重要。如果是多个字段为复合唯一约束则定义多个<id>。</id></id></li>
<li>property：表示Orders类的属性。</li>
<li>column：表示sql查询出来的字段名。<br>column和property放在一块儿表示将sql查询出来的字段映射到指定的pojo类属性上。</li>
<li><result>：普通列使用result标签映射。</result></li>
</ul>
<p><strong>解决办法：</strong><br>将OrderMapper.xml文件中的<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">&lt;select id=&quot;getOrderList&quot; resultType=&quot;order&quot;&gt;</span><br><span class="line">    select * from order </span><br><span class="line">&lt;/select&gt;</span><br></pre></td></tr></table></figure></p>
<p>修改为<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">&lt;resultMap type=&quot;order&quot; id=&quot;order_list_result_map&quot;&gt;</span><br><span class="line">	&lt;!-- id是主键的映射，其中property是pojo中主键的属性，column是返回主键的列 --&gt;</span><br><span class="line">	&lt;id property=&quot;id&quot; column=&quot;id&quot;/&gt;</span><br><span class="line">	&lt;!-- 普通列使用result映射 --&gt;</span><br><span class="line">	&lt;result property=&quot;userId&quot; column=&quot;user_id&quot;/&gt;</span><br><span class="line">	&lt;result property=&quot;number&quot; column=&quot;number&quot;/&gt;</span><br><span class="line">	&lt;result property=&quot;createtime&quot; column=&quot;createtime&quot;/&gt;</span><br><span class="line">	&lt;result property=&quot;note&quot; column=&quot;note&quot;/&gt;</span><br><span class="line">&lt;/resultMap&gt;</span><br><span class="line">&lt;!-- 使用resultMap方式 --&gt;</span><br><span class="line">&lt;select id=&quot;getOrderList&quot; resultMap=&quot;order_list_result_map&quot;&gt;</span><br><span class="line">	select * from orders</span><br><span class="line">&lt;/select&gt;</span><br></pre></td></tr></table></figure></p>
<p>执行测试并打印：<br><img src="https://upload-images.jianshu.io/upload_images/3110861-14d28cb3a500d498.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图17.png"></p>
<p>####6. 动态SQL</p>
<p>######1). if标签<br>I. 在UserMapper.xml添加方法<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span><br><span class="line">&lt;!DOCTYPE mapper PUBLIC &quot;-//mybatis.org//DTD Mapper 3.0//EN&quot; &quot;http://mybatis.org/dtd/mybatis-3-mapper.dtd&quot;&gt;</span><br><span class="line">&lt;mapper namespace=&quot;com.mazaiting.mapper.UserMapper&quot;&gt;</span><br><span class="line">	&lt;select id=&quot;findUserList&quot; parameterType=&quot;user&quot; resultType=&quot;user&quot;&gt;</span><br><span class="line">		select * from user</span><br><span class="line">		where 1=1 </span><br><span class="line">		&lt;if test=&quot;id!=null&quot;&gt;</span><br><span class="line">			and id = #&#123;id&#125;</span><br><span class="line">		&lt;/if&gt;	</span><br><span class="line">		&lt;if test=&quot;username!=null and username !=&apos;&apos;&quot;&gt;</span><br><span class="line">			and username like &apos;%$&#123;username&#125;%&apos;</span><br><span class="line">		&lt;/if&gt;</span><br><span class="line">	&lt;/select&gt;</span><br><span class="line">&lt;/mapper&gt;</span><br></pre></td></tr></table></figure></p>
<p><strong>注意：</strong></p>
<ul>
<li>username要做不等于空字符串的校验。</li>
<li>User类中id属性的类型要改为Integer包装类型，因为int类型的id是不可能为null的！</li>
</ul>
<p>II. 在UserMapper中添加方法<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">public interface UserMapper &#123;</span><br><span class="line">	List&lt;User&gt; findUserList(User user);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>III. 测试方法<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line">public class UserMapperTest &#123;</span><br><span class="line">	// 单例工厂</span><br><span class="line">	private SqlSessionFactory factory;</span><br><span class="line">	@Before</span><br><span class="line">	public void init() throws IOException &#123;</span><br><span class="line">		// 1. 创建SqlSessionFactoryBuilder对象</span><br><span class="line">		SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();</span><br><span class="line">		// 2. 加载配置文件</span><br><span class="line">		InputStream inputStream = Resources.getResourceAsStream(&quot;SqlMapConfig.xml&quot;);</span><br><span class="line">		// 3. 创建SqlSessionFactory对象</span><br><span class="line">		factory = builder.build(inputStream);</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	@Test</span><br><span class="line">	public void testFindUserList()&#123;</span><br><span class="line">		SqlSession session = factory.openSession();</span><br><span class="line">		UserMapper mapper = session.getMapper(UserMapper.class);</span><br><span class="line">		User user = new User();</span><br><span class="line">		user.setId(10);</span><br><span class="line">		List&lt;User&gt; list = mapper.findUserList(user);</span><br><span class="line">		for (User nUser : list) &#123;</span><br><span class="line">			System.out.println(nUser);</span><br><span class="line">		&#125;</span><br><span class="line">		session.close();</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>IV. 执行测试方法，打印结果：<br><img src="https://upload-images.jianshu.io/upload_images/3110861-d25ed0bda09f84ee.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图18.png"></p>
<p>######2). where标签<br>可将上个例子中的UserMapper.xml文件中<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">&lt;select id=&quot;findUserList&quot; parameterType=&quot;user&quot; resultType=&quot;user&quot;&gt;</span><br><span class="line">	select * from user</span><br><span class="line">	where 1=1 </span><br><span class="line">	&lt;if test=&quot;id!=null&quot;&gt;</span><br><span class="line">		and id = #&#123;id&#125;</span><br><span class="line">	&lt;/if&gt;	</span><br><span class="line">	&lt;if test=&quot;username!=null and username !=&apos;&apos;&quot;&gt;</span><br><span class="line">		and username like &apos;%$&#123;username&#125;%&apos;</span><br><span class="line">	&lt;/if&gt;</span><br><span class="line">&lt;/select&gt;</span><br></pre></td></tr></table></figure></p>
<p>修改为：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">&lt;select id=&quot;findUserList&quot; parameterType=&quot;user&quot; resultType=&quot;user&quot;&gt;</span><br><span class="line">	select * from user</span><br><span class="line">	&lt;where&gt;</span><br><span class="line">		&lt;if test=&quot;id!=null&quot;&gt;</span><br><span class="line">			and id = #&#123;id&#125;</span><br><span class="line">		&lt;/if&gt;</span><br><span class="line">		&lt;if test=&quot;username!=null and username !=&apos;&apos;&quot;&gt;</span><br><span class="line">			and username like &apos;%$&#123;username&#125;%&apos;</span><br><span class="line">		&lt;/if&gt;</span><br><span class="line">	&lt;/where&gt;</span><br><span class="line">&lt;/select&gt;</span><br></pre></td></tr></table></figure></p>
<p>执行测试代码,打印结果：<br><img src="https://upload-images.jianshu.io/upload_images/3110861-88db18f044c0c6f6.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图19.png"></p>
<p>######3). foreach<br>I. 在QueryVo类中添加id列表属性<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">public class QueryVo &#123;</span><br><span class="line">	private User user;</span><br><span class="line">	private List&lt;Integer&gt; ids;</span><br><span class="line">	public void setUser(User user) &#123;</span><br><span class="line">		this.user = user;</span><br><span class="line">	&#125;</span><br><span class="line">	public User getUser() &#123;</span><br><span class="line">		return user;</span><br><span class="line">	&#125;</span><br><span class="line">	public void setIds(List&lt;Integer&gt; ids) &#123;</span><br><span class="line">		this.ids = ids;</span><br><span class="line">	&#125;</span><br><span class="line">	public List&lt;Integer&gt; getIds() &#123;</span><br><span class="line">		return ids;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>II. 在UserMapper.xml文件中添加查询<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span><br><span class="line">&lt;!DOCTYPE mapper PUBLIC &quot;-//mybatis.org//DTD Mapper 3.0//EN&quot; &quot;http://mybatis.org/dtd/mybatis-3-mapper.dtd&quot;&gt;</span><br><span class="line">&lt;mapper namespace=&quot;com.mazaiting.mapper.UserMapper&quot;&gt;</span><br><span class="line">	&lt;!-- 动态sql foreach测试 --&gt;</span><br><span class="line">	&lt;select id=&quot;findUserByIds&quot; parameterType=&quot;queryvo&quot; resultType=&quot;user&quot;&gt;</span><br><span class="line">		select * from user</span><br><span class="line">		&lt;where&gt;</span><br><span class="line">			&lt;!-- and id in(1,10,20,21,31)</span><br><span class="line">				collection  遍历的集合</span><br><span class="line">				item		条目</span><br><span class="line">				open 		循环之前的内容</span><br><span class="line">				close		循环之后的内容</span><br><span class="line">				separator	分隔符</span><br><span class="line">			 --&gt;</span><br><span class="line">			&lt;foreach collection=&quot;ids&quot; item=&quot;id&quot; open=&quot;and id in(&quot; close=&quot;)&quot; separator=&quot;,&quot;&gt;</span><br><span class="line">				#&#123;id&#125;</span><br><span class="line">			&lt;/foreach&gt;</span><br><span class="line">		&lt;/where&gt;</span><br><span class="line">	&lt;/select&gt;</span><br><span class="line">&lt;/mapper&gt;</span><br></pre></td></tr></table></figure></p>
<p>III. 在UserMapper接口中添加方法<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">public interface UserMapper &#123;</span><br><span class="line">	List&lt;User&gt; findUserByIds(QueryVo queryVo);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>IV. 测试方法<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line">public class UserMapperTest &#123;</span><br><span class="line">	// 单例工厂</span><br><span class="line">	private SqlSessionFactory factory;</span><br><span class="line">	@Before</span><br><span class="line">	public void init() throws IOException &#123;</span><br><span class="line">		// 1. 创建SqlSessionFactoryBuilder对象</span><br><span class="line">		SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();</span><br><span class="line">		// 2. 加载配置文件</span><br><span class="line">		InputStream inputStream = Resources.getResourceAsStream(&quot;SqlMapConfig.xml&quot;);</span><br><span class="line">		// 3. 创建SqlSessionFactory对象</span><br><span class="line">		factory = builder.build(inputStream);</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	@Test</span><br><span class="line">	public void testFindUserByIds()&#123;</span><br><span class="line">		SqlSession session = factory.openSession();</span><br><span class="line">		UserMapper mapper = session.getMapper(UserMapper.class);</span><br><span class="line">		QueryVo queryVo = new QueryVo();</span><br><span class="line">		List&lt;Integer&gt; list = new ArrayList&lt;&gt;();</span><br><span class="line">		list.add(1);</span><br><span class="line">		list.add(10);</span><br><span class="line">		list.add(16);</span><br><span class="line">		list.add(22);</span><br><span class="line">		queryVo.setIds(list);</span><br><span class="line">		List&lt;User&gt; userList = mapper.findUserByIds(queryVo);</span><br><span class="line">		for (User user : userList) &#123;</span><br><span class="line">			System.out.println(user);</span><br><span class="line">		&#125;</span><br><span class="line">		session.close();</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>V. 测试方法打印结果：<br><img src="https://upload-images.jianshu.io/upload_images/3110861-8cfabb3620b5e67e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图20.png"></p>
<p>######4). sql片段<br>sql中可将重复的sql提取出来，使用时用include引用即可，最终达到sql重用的目的<br>将下面的select方法使用sql片段：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">&lt;select id=&quot;findUserList&quot; parameterType=&quot;user&quot; resultType=&quot;user&quot;&gt;</span><br><span class="line">	select * from user</span><br><span class="line">	&lt;where&gt;</span><br><span class="line">		&lt;if test=&quot;id!=null&quot;&gt;</span><br><span class="line">			and id = #&#123;id&#125;</span><br><span class="line">		&lt;/if&gt;</span><br><span class="line">		&lt;if test=&quot;username!=null and username !=&apos;&apos;&quot;&gt;</span><br><span class="line">			and username like &apos;%$&#123;username&#125;%&apos;</span><br><span class="line">		&lt;/if&gt;</span><br><span class="line">	&lt;/where&gt;</span><br><span class="line">&lt;/select&gt;</span><br></pre></td></tr></table></figure></p>
<p>修改为：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">&lt;!-- 查询字段 --&gt;</span><br><span class="line">&lt;sql id=&quot;user_field_list&quot;&gt;</span><br><span class="line">	id,username,birthday,sex,address</span><br><span class="line">&lt;/sql&gt;</span><br><span class="line">&lt;!-- 抽取where条件 --&gt;</span><br><span class="line">&lt;sql id=&quot;find_user_list_where&quot;&gt;</span><br><span class="line">	&lt;where&gt;</span><br><span class="line">		&lt;if test=&quot;id!=null&quot;&gt;</span><br><span class="line">			and id = #&#123;id&#125;</span><br><span class="line">		&lt;/if&gt;</span><br><span class="line">		&lt;if test=&quot;username!=null and username !=&apos;&apos;&quot;&gt;</span><br><span class="line">			and username like &apos;%$&#123;username&#125;%&apos;</span><br><span class="line">		&lt;/if&gt;</span><br><span class="line">	&lt;/where&gt;</span><br><span class="line">&lt;/sql&gt;</span><br><span class="line"></span><br><span class="line">&lt;select id=&quot;findUserList&quot; parameterType=&quot;user&quot; resultType=&quot;user&quot;&gt;</span><br><span class="line">	select &lt;include refid=&quot;user_field_list&quot;/&gt; from user</span><br><span class="line">	&lt;include refid=&quot;find_user_list_where&quot;/&gt;</span><br><span class="line">&lt;/select&gt;</span><br></pre></td></tr></table></figure></p>
<p>######5).注意：如果引用其它mapper.xml映射文件的sql片段，则在引用时需要加上namespace，如下：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&lt;include refid=&quot;namespace.sql片段id&quot;/&gt;</span><br></pre></td></tr></table></figure></p>
<p>####7. 一对一关联映射</p>
<p>######1). 一对一查询–方法一<br>I. 创建OrderUser类，并继承Order类<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">public class OrderUser extends Order&#123;</span><br><span class="line">	private String username;</span><br><span class="line">	private String address;</span><br><span class="line">	public String getUsername() &#123;</span><br><span class="line">		return username;</span><br><span class="line">	&#125;</span><br><span class="line">	public void setUsername(String username) &#123;</span><br><span class="line">		this.username = username;</span><br><span class="line">	&#125;</span><br><span class="line">	public String getAddress() &#123;</span><br><span class="line">		return address;</span><br><span class="line">	&#125;</span><br><span class="line">	public void setAddress(String address) &#123;</span><br><span class="line">		this.address = address;</span><br><span class="line">	&#125;</span><br><span class="line">	@Override</span><br><span class="line">	public String toString() &#123;</span><br><span class="line">		return &quot;OrderUser [username=&quot; + username + &quot;, address=&quot; + address + &quot;, getId()=&quot; + getId() + &quot;, getUserId()=&quot;</span><br><span class="line">				+ getUserId() + &quot;, getNumber()=&quot; + getNumber() + &quot;, getCreatetime()=&quot; + getCreatetime() + &quot;, getNote()=&quot;</span><br><span class="line">				+ getNote();</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>II. 在OrderMapper.xml文件中添加查询方法<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span><br><span class="line">&lt;!DOCTYPE mapper PUBLIC &quot;-//mybatis.org//DTD Mapper 3.0//EN&quot; &quot;http://mybatis.org/dtd/mybatis-3-mapper.dtd&quot;&gt;</span><br><span class="line">&lt;mapper namespace=&quot;com.mazaiting.mapper.OrderMapper&quot;&gt;</span><br><span class="line">	</span><br><span class="line">	&lt;select id=&quot;getOrderUserList&quot; resultType=&quot;orderuser&quot;&gt;</span><br><span class="line">		select </span><br><span class="line">			o.id,</span><br><span class="line">			o.user_id userId,</span><br><span class="line">			o.number,</span><br><span class="line">			o.createtime,</span><br><span class="line">			o.note,</span><br><span class="line">			u.username,</span><br><span class="line">			u.address</span><br><span class="line">		from</span><br><span class="line">			orders o</span><br><span class="line">		left join user u on o.user_id = u.id</span><br><span class="line">	&lt;/select&gt;</span><br><span class="line"></span><br><span class="line">&lt;/mapper&gt;</span><br></pre></td></tr></table></figure></p>
<p>III. 在OrderMapper中添加方法<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">public interface OrderMapper &#123;</span><br><span class="line">	List&lt;OrderUser&gt; getOrderUserList();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>IV. 测试方法<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">public class OrderMapperTest &#123;</span><br><span class="line">	SqlSessionFactory factory;</span><br><span class="line">	@Before</span><br><span class="line">	public void init() throws IOException &#123;</span><br><span class="line">		SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();</span><br><span class="line">		InputStream inputStream = Resources.getResourceAsStream(&quot;SqlMapConfig.xml&quot;);</span><br><span class="line">		factory = builder.build(inputStream);</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	@Test</span><br><span class="line">	public void testGetOrderUserList()&#123;</span><br><span class="line">		SqlSession session = factory.openSession();</span><br><span class="line">		OrderMapper mapper = session.getMapper(OrderMapper.class);</span><br><span class="line">		List&lt;OrderUser&gt; list = mapper.getOrderUserList();</span><br><span class="line">		for (OrderUser orderUser : list) &#123;</span><br><span class="line">			System.out.println(orderUser.toString());</span><br><span class="line">		&#125;</span><br><span class="line">		session.close();</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>V. 打印结果：<br><img src="https://upload-images.jianshu.io/upload_images/3110861-2f015dc199eb2b2f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图21.png"><br><strong>注意：定义专门的po类作为输出类型，其中定义了sql查询结果集所有的字段。此方法较为简单，企业中使用普遍。</strong></p>
<p>######2).  一对一查询–方法二<br>使用resultMap，定义专门的resultMap用于映射一对一查询结果。首先在Order类中加入user属性，user属性中用于存储关联查询的用户信息，因为订单关联查询用户是一对一关系，所以这里使用单个User对象存储关联查询的用户信息。<br>I. 修改Order类：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br></pre></td><td class="code"><pre><span class="line">public class Order &#123;</span><br><span class="line">	private Integer id;</span><br><span class="line">	private Integer userId;</span><br><span class="line">	private String number;</span><br><span class="line">	private Date createtime;</span><br><span class="line">	private String note;</span><br><span class="line">	private User user;</span><br><span class="line">	public Integer getId() &#123;</span><br><span class="line">		return id;</span><br><span class="line">	&#125;</span><br><span class="line">	public void setId(Integer id) &#123;</span><br><span class="line">		this.id = id;</span><br><span class="line">	&#125;</span><br><span class="line">	public Integer getUserId() &#123;</span><br><span class="line">		return userId;</span><br><span class="line">	&#125;</span><br><span class="line">	public void setUserId(Integer userId) &#123;</span><br><span class="line">		this.userId = userId;</span><br><span class="line">	&#125;</span><br><span class="line">	public String getNumber() &#123;</span><br><span class="line">		return number;</span><br><span class="line">	&#125;</span><br><span class="line">	public void setNumber(String number) &#123;</span><br><span class="line">		this.number = number;</span><br><span class="line">	&#125;</span><br><span class="line">	public Date getCreatetime() &#123;</span><br><span class="line">		return createtime;</span><br><span class="line">	&#125;</span><br><span class="line">	public void setCreatetime(Date createtime) &#123;</span><br><span class="line">		this.createtime = createtime;</span><br><span class="line">	&#125;</span><br><span class="line">	public String getNote() &#123;</span><br><span class="line">		return note;</span><br><span class="line">	&#125;</span><br><span class="line">	public void setNote(String note) &#123;</span><br><span class="line">		this.note = note;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	public User getUser() &#123;</span><br><span class="line">		return user;</span><br><span class="line">	&#125;</span><br><span class="line">	public void setUser(User user) &#123;</span><br><span class="line">		this.user = user;</span><br><span class="line">	&#125;</span><br><span class="line">	@Override</span><br><span class="line">	public String toString() &#123;</span><br><span class="line">		return &quot;Order [id=&quot; + id + &quot;, userId=&quot; + userId + &quot;, number=&quot; + number + &quot;, createtime=&quot; + createtime</span><br><span class="line">				+ &quot;, note=&quot; + note + &quot;, user=&quot; + user + &quot;]&quot;;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>II. 在OrderMapper.xml文件中添加查询方法<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span><br><span class="line">&lt;!DOCTYPE mapper PUBLIC &quot;-//mybatis.org//DTD Mapper 3.0//EN&quot; &quot;http://mybatis.org/dtd/mybatis-3-mapper.dtd&quot;&gt;</span><br><span class="line">&lt;mapper namespace=&quot;com.mazaiting.mapper.OrderMapper&quot;&gt;</span><br><span class="line">	&lt;resultMap type=&quot;order&quot; id=&quot;order_user_resultmap&quot;&gt;</span><br><span class="line">		&lt;id property=&quot;id&quot; column=&quot;id&quot;/&gt;</span><br><span class="line">		&lt;result property=&quot;userId&quot; column=&quot;user_id&quot;/&gt;</span><br><span class="line">		&lt;result property=&quot;number&quot; column=&quot;number&quot;/&gt;</span><br><span class="line">		&lt;result property=&quot;createtime&quot; column=&quot;createtime&quot;/&gt;</span><br><span class="line">		&lt;result property=&quot;note&quot; column=&quot;note&quot;/&gt;</span><br><span class="line">		&lt;!-- 配置一对一关联映射 --&gt;</span><br><span class="line">		&lt;association property=&quot;user&quot; javaType=&quot;com.mazaiting.po.User&quot;&gt;</span><br><span class="line">			&lt;id property=&quot;id&quot; column=&quot;user_id&quot;/&gt;</span><br><span class="line">			&lt;result property=&quot;username&quot; column=&quot;username&quot;/&gt;</span><br><span class="line">			&lt;result property=&quot;address&quot; column=&quot;address&quot;/&gt;</span><br><span class="line">		&lt;/association&gt;</span><br><span class="line">	&lt;/resultMap&gt;</span><br><span class="line"></span><br><span class="line">	&lt;select id=&quot;getOrderUserResultMap&quot; resultMap=&quot;order_user_resultmap&quot;&gt;</span><br><span class="line">		select</span><br><span class="line">			o.id,</span><br><span class="line">			o.user_id,</span><br><span class="line">			o.number,</span><br><span class="line">			o.createtime,</span><br><span class="line">			o.note,</span><br><span class="line">			u.username,</span><br><span class="line">			u.address</span><br><span class="line">		from</span><br><span class="line">			orders o</span><br><span class="line">		left join user u on o.user_id = u.id</span><br><span class="line">	&lt;/select&gt;</span><br><span class="line"></span><br><span class="line">&lt;/mapper&gt;</span><br></pre></td></tr></table></figure></p>
<ul>
<li>association：表示进行关联查询单条记录。</li>
<li>property：表示关联查询的结果存储在com.mazaiting.po.Order的user属性中。即property对应Orders类里面一对一关联映射的那个属性，即user属性。</li>
<li>javaType：表示关联查询的结果类型。即user属性的数据类型，可使用别名。</li>
<li><code>&lt;id property=&quot;id&quot; column=&quot;user_id&quot;/&gt;</code>：查询结果的user_id列对应关联对象的id属性，这里是<id>表示user_id是关联查询对象的唯一标识。</id></li>
<li><code>&lt;result property=&quot;username&quot; column=&quot;username&quot;/&gt;</code>：查询结果的username列对应关联对象的username属性。</li>
</ul>
<p>III. 在OrderMapper接口中完成方法<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">public interface OrderMapper &#123;</span><br><span class="line">	List&lt;Order&gt; getOrderUserResultMap();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>IV. 测试方法<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">public class OrderMapperTest &#123;</span><br><span class="line">	SqlSessionFactory factory;</span><br><span class="line">	@Before</span><br><span class="line">	public void init() throws IOException &#123;</span><br><span class="line">		SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();</span><br><span class="line">		InputStream inputStream = Resources.getResourceAsStream(&quot;SqlMapConfig.xml&quot;);</span><br><span class="line">		factory = builder.build(inputStream);</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	@Test</span><br><span class="line">	public void testGetOrderUserResultMap()&#123;</span><br><span class="line">		SqlSession session = factory.openSession();</span><br><span class="line">		OrderMapper mapper = session.getMapper(OrderMapper.class);</span><br><span class="line">		List&lt;Order&gt; list = mapper.getOrderUserResultMap();</span><br><span class="line">		for (Order order : list) &#123;</span><br><span class="line">			System.out.println(order.toString());</span><br><span class="line">		&#125;</span><br><span class="line">		session.close();</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>V. 打印结果：<br><img src="https://upload-images.jianshu.io/upload_images/3110861-18a8122184cf91bb.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图22.png"></p>
<p><strong>总结：使用association完成关联查询，将关联查询信息映射到pojo对象中。</strong></p>
<p>######3). 一对多关联映射<br>I. 在User类中加入List<orders> orders属性<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br></pre></td><td class="code"><pre><span class="line">public class User &#123;</span><br><span class="line">	// id</span><br><span class="line">	private int id;</span><br><span class="line">	// 用户名</span><br><span class="line">	private String username;</span><br><span class="line">	// 性别</span><br><span class="line">	private String sex;</span><br><span class="line">	// 用户名</span><br><span class="line">	private Date birthday;</span><br><span class="line">	// 地址</span><br><span class="line">	private String address;</span><br><span class="line">	private List&lt;Order&gt; orders;</span><br><span class="line">	public int getId() &#123;</span><br><span class="line">		return id;</span><br><span class="line">	&#125;</span><br><span class="line">	public void setId(int id) &#123;</span><br><span class="line">		this.id = id;</span><br><span class="line">	&#125;</span><br><span class="line">	public String getUsername() &#123;</span><br><span class="line">		return username;</span><br><span class="line">	&#125;</span><br><span class="line">	public void setUsername(String username) &#123;</span><br><span class="line">		this.username = username;</span><br><span class="line">	&#125;</span><br><span class="line">	public String getSex() &#123;</span><br><span class="line">		return sex;</span><br><span class="line">	&#125;</span><br><span class="line">	public void setSex(String sex) &#123;</span><br><span class="line">		this.sex = sex;</span><br><span class="line">	&#125;</span><br><span class="line">	public Date getBirthday() &#123;</span><br><span class="line">		return birthday;</span><br><span class="line">	&#125;</span><br><span class="line">	public void setBirthday(Date birthday) &#123;</span><br><span class="line">		this.birthday = birthday;</span><br><span class="line">	&#125;</span><br><span class="line">	public String getAddress() &#123;</span><br><span class="line">		return address;</span><br><span class="line">	&#125;</span><br><span class="line">	public void setAddress(String address) &#123;</span><br><span class="line">		this.address = address;</span><br><span class="line">	&#125;</span><br><span class="line">	public List&lt;Order&gt; getOrders() &#123;</span><br><span class="line">		return orders;</span><br><span class="line">	&#125;</span><br><span class="line">	public void setOrders(List&lt;Order&gt; orders) &#123;</span><br><span class="line">		this.orders = orders;</span><br><span class="line">	&#125;</span><br><span class="line">	@Override</span><br><span class="line">	public String toString() &#123;</span><br><span class="line">		return &quot;User [id=&quot; + id + &quot;, username=&quot; + username + &quot;, sex=&quot; + sex + &quot;, birthday=&quot; + birthday + &quot;, address=&quot;</span><br><span class="line">				+ address + &quot;, orders=&quot; + orders + &quot;]&quot;;</span><br><span class="line">	&#125;	</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></orders></p>
<p>II. 在UserMapper.xml文件中添加查询方法<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span><br><span class="line">&lt;!DOCTYPE mapper PUBLIC &quot;-//mybatis.org//DTD Mapper 3.0//EN&quot; &quot;http://mybatis.org/dtd/mybatis-3-mapper.dtd&quot;&gt;</span><br><span class="line">&lt;mapper namespace=&quot;com.mazaiting.mapper.UserMapper&quot;&gt;</span><br><span class="line">	&lt;resultMap type=&quot;user&quot; id=&quot;user_order_resultmap&quot;&gt;</span><br><span class="line">		&lt;id property=&quot;id&quot; column=&quot;id&quot;/&gt;</span><br><span class="line">		&lt;result property=&quot;username&quot; column=&quot;username&quot;/&gt;</span><br><span class="line">		&lt;result property=&quot;birthday&quot; column=&quot;birthday&quot;/&gt;</span><br><span class="line">		&lt;result property=&quot;sex&quot; column=&quot;sex&quot;/&gt;</span><br><span class="line">		&lt;result property=&quot;address&quot; column=&quot;address&quot;/&gt;</span><br><span class="line">		&lt;!-- 配置一对多映射 --&gt;</span><br><span class="line">		&lt;collection property=&quot;orders&quot; ofType=&quot;order&quot;&gt;</span><br><span class="line">			&lt;id property=&quot;id&quot; column=&quot;oid&quot;/&gt;</span><br><span class="line">			&lt;result property=&quot;number&quot; column=&quot;number&quot;/&gt;</span><br><span class="line">			&lt;result property=&quot;createtime&quot; column=&quot;createtime&quot;/&gt;</span><br><span class="line">			&lt;result property=&quot;note&quot; column=&quot;note&quot;/&gt;</span><br><span class="line">		&lt;/collection&gt;</span><br><span class="line">	&lt;/resultMap&gt;</span><br><span class="line">	</span><br><span class="line">	&lt;select id=&quot;getUserWithOrders&quot; resultMap=&quot;user_order_resultmap&quot;&gt;</span><br><span class="line">		select</span><br><span class="line">			u.id,</span><br><span class="line">			u.username,</span><br><span class="line">			u.birthday,</span><br><span class="line">			u.sex,</span><br><span class="line">			u.address,</span><br><span class="line">			o.id oid,</span><br><span class="line">			o.number,</span><br><span class="line">			o.createtime,</span><br><span class="line">			o.note</span><br><span class="line">		from</span><br><span class="line">			user u</span><br><span class="line">		left join orders o on u.id = o.user_id</span><br><span class="line">	&lt;/select&gt;</span><br><span class="line">&lt;/mapper&gt;</span><br></pre></td></tr></table></figure></p>
<ul>
<li>collection部分定义了用户关联的订单信息。表示关联查询结果集。</li>
<li>property=”orders”：关联查询的结果集存储在User对象的哪个属性上。即property对应User对象中的集合属性。</li>
<li>ofType=”order”：指定关联查询的结果集中的对象类型即List中的对象类型。此处可以使用别名，也可以使用全限定名。</li>
<li><id>及<result>的意义同一对一查询。<br>III. 在UserMapper接口中添加方法<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">public interface UserMapper &#123;</span><br><span class="line">	List&lt;User&gt; getUserWithOrders();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</result></id></li>
</ul>
<p>IV. 测试方法<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">public class UserMapperTest &#123;</span><br><span class="line">	// 单例工厂</span><br><span class="line">	private SqlSessionFactory factory;</span><br><span class="line">	@Before</span><br><span class="line">	public void init() throws IOException &#123;</span><br><span class="line">		// 1. 创建SqlSessionFactoryBuilder对象</span><br><span class="line">		SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();</span><br><span class="line">		// 2. 加载配置文件</span><br><span class="line">		InputStream inputStream = Resources.getResourceAsStream(&quot;SqlMapConfig.xml&quot;);</span><br><span class="line">		// 3. 创建SqlSessionFactory对象</span><br><span class="line">		factory = builder.build(inputStream);</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	@Test</span><br><span class="line">	public void testGetUserWithOrders()&#123;</span><br><span class="line">		SqlSession session = factory.openSession();</span><br><span class="line">		UserMapper mapper = session.getMapper(UserMapper.class);</span><br><span class="line">		List&lt;User&gt; list =mapper.getUserWithOrders();</span><br><span class="line">		for (User user : list) &#123;</span><br><span class="line">			System.out.println(user.toString());</span><br><span class="line">		&#125;</span><br><span class="line">		session.close();</span><br><span class="line">	&#125;</span><br><span class="line">		</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>V. 执行测试，打印结果：<br><img src="https://upload-images.jianshu.io/upload_images/3110861-5b209fab945d2e5c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图23.png"></p>
<p>###<a href="https://download.csdn.net/download/mazaiting/10352107" target="_blank" rel="noopener">代码下载</a></p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/blog/2018/08/06/JakartaEE-Framework/JavaEE-Hibernate注解使用/" rel="next" title="JavaEE-Hibernate注解使用">
                <i class="fa fa-chevron-left"></i> JavaEE-Hibernate注解使用
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/blog/2018/08/06/JakartaEE-Framework/JavaEE-Mybatis使用/" rel="prev" title="JavaEE-Mybatis使用">
                JavaEE-Mybatis使用 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      

      <section class="site-overview-wrap sidebar-panel sidebar-panel-active">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/blog/images/avatar.jpeg"
                alt="凌浩雨" />
            
              <p class="site-author-name" itemprop="name">凌浩雨</p>
              <p class="site-description motion-element" itemprop="description">专业从事于移动端开发，Android与iOS均有涉及，主做Android开发与React Native开发</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/blog/archives/">
              
                  <span class="site-state-item-count">393</span>
                  <span class="site-state-item-name">posts</span>
                </a>
              </div>
            

            

            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2018</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">凌浩雨</span>

  
</div>


  <div class="powered-by">Powered by <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a></div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">Theme &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Pisces</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/blog/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/blog/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/blog/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/blog/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/blog/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/blog/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
